// pages/jigsaw/jigsaw.js
var isReset = false
var wtimes = 1
wx.getSystemInfo({
  success: function (res) {
    wtimes = res.windowWidth / 750
  },
})
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dis: [true, true, true, true],
    bgcolor: ["#f00", "#ff0", "#0f0", "#00f"],
    x: [40, 80, 120, 160],
    y: [20, 40, 60, 80],
    xend: [38, 98, 238, 318],
    yend: [215, 395, 175, 315],
  },
  move: function (e) {
    if (isReset) {
      return
    }
    var x = e.detail.x / wtimes
    var xx = this.data.x
    var y = e.detail.y / wtimes
    var yy = this.data.y
    var d = false
    var dd = this.data.dis
    var n = Number(e.target.dataset.val)
    if (Math.abs(this.data.xend[n] - x) < 50 && Math.abs(this.data.yend[n] - y) < 50) {
      isReset = true
      x = this.data.xend[n]
      y = this.data.yend[n]
      d = true
    }
    xx[n] = x
    yy[n] = y
    dd[n] = d
    this.setData({
      x: xx,
      y: yy,
      dis: dd,
    })
    isReset = false
  },
  resetBtn: function () {
    isReset = true
    var dis = [true, true, true, true]
    var bgcolor = ["#f00", "#ff0", "#0f0", "#00f"]
    var x = [40, 80, 120, 160]
    var y = [20, 40, 60, 80]
    this.setData({
      dis: dis,
      bgcolor: bgcolor,
      x: x,
      y: y
    })
  },
  playBtn: function () {
    if (this.data.x.toString() === this.data.xend.toString() && this.data.y.toString() === this.data.yend.toString()) {
      return
    }
    for (var i = 0; i < 4; i++) {
      if (this.data.x[i] == this.data.xend[i] & this.data.y[i] == this.data.yend[i]) {
        return
      }
    }
    isReset = false
    var dis = [false, false, false, false]
    this.setData({
      dis: dis
    })
  },
  doneBtn: function () {
    var bg = ["#ccc", "#ccc", "#ccc", "#ccc"]
    if (this.data.x.toString() === this.data.xend.toString() && this.data.y.toString() === this.data.yend.toString()) {
      this.setData({
        bgcolor: bg
      })
    } else {
      return
    }
  },
})